package org.smart4j.framework.helper;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * description: 数据库操作助手类
 * @version v1.0
 * @author w
 * @date 2018年10月20日下午6:00:20
 **/
public class DatabaseHelper {
	
	private static final Logger logger = LoggerFactory.getLogger(DatabaseHelper.class);
	
	private static final ThreadLocal<Connection> connectionContainer ;
	
	private static final QueryRunner QUERY_RUNNER ;
	
	private static final BasicDataSource DATA_SOURCE ;
	
	static{
		connectionContainer = new ThreadLocal<Connection>();
		QUERY_RUNNER = new QueryRunner();
		DATA_SOURCE = new BasicDataSource();
		DATA_SOURCE.setDriverClassName(ConfigHelper.getJdbcDriver());
		DATA_SOURCE.setUrl(ConfigHelper.getJdbcURL());
		DATA_SOURCE.setUsername(ConfigHelper.getJdbcUsername());
		DATA_SOURCE.setPassword(ConfigHelper.getJdbcPassword());
	}
	
	/**
	 * description: 获取数据源对象 dataSource
	 * @return DataSource
	 * @version v1.0
	 * @author w
	 * @date 2018年11月4日 下午10:13:45
	 */
	public static DataSource getDataSource(){
		return DATA_SOURCE ;
	}
	
	
	/**
	 * description: 获取数据库连接对象 
	 * @return Connection
	 * @version v1.0
	 * @author w
	 * @date 2018年11月4日 下午10:31:03
	 */
	public static Connection  getConnection() {
		Connection conn = connectionContainer.get();
		if(conn == null){
			try {
				conn = DATA_SOURCE.getConnection();
			} catch (SQLException e) {
				logger.error("get connection failure " , e);
				throw new RuntimeException(e);
			}finally{
				connectionContainer.set(conn);
			}
		}
		return conn;
	}
	
	/**
	 * description: 开启事务
	 * @return void
	 * @version v1.0
	 * @author w
	 * @date 2018年10月20日 下午6:15:24
	 */
	public static void beginTransaction(){
		Connection connection = getConnection();
		
		if (null != connection) {
			try {
				// autoCommit value default true
				connection.setAutoCommit(false);
			} catch (SQLException e) {
				logger.error("begin transaction failure ", e);
				new RuntimeException(e);
			}finally {
				connectionContainer.set(connection);
			}
		}
		
	} 
	
	/**
	 * description: 提交事务
	 * @return void
	 * @version v1.0
	 * @author w
	 * @date 2018年10月20日 下午6:15:50
	 */
	public static void commitTransaction(){
		Connection connection = getConnection();
		if(null != connection){
			try {
				connection.commit();
				// remember close connection
				connection.close();
			} catch (SQLException e) {
				logger.error(" commit transaction failure ",e);
				new RuntimeException(e);
			}finally{
				connectionContainer.remove();
			}
		}
	}
	
	/**
	 * description: 回滚事务
	 * @return void
	 * @version v1.0
	 * @author w
	 * @date 2018年10月20日 下午6:26:29
	 */
	public static void rollBackTransaction(){
		Connection connection = getConnection();
		if(null != connection){
			try {
				connection.rollback();
				connection.close();
			} catch (SQLException e) {
				logger.error("rollBack transaction failure ", e);
				new RuntimeException(e);
			}finally {
				connectionContainer.remove();
			}
		}
	} // end rollBack transaction
	
	
	
}
